<template>
  <a-card :bordered="false" class="card-area ccslh">
    <div class="table-page-search-wrapper" v-if="!visible">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <!-- <a-col :md="6" :sm="12">
            <a-form-item label="开始时间">
              <a-date-picker v-model="queryParam.start_time" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="结束时间">
              <a-date-picker v-model="queryParam.ent_time" />
            </a-form-item>
          </a-col> -->
          <a-col :md="6" :sm="12">
              <a-form-item label="院系">
                <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.yxm" @change="yxChange">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
              <a-form-item label="专业">
                <a-select :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.zym">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
              <a-form-item label="审核状态">
                <a-select allowClear v-model="queryParam.autstate">
                  <a-select-option v-for="i in TYPE0402" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
          </a-col>
          <a-col :md="8" :sm="16">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button type="primary" v-hasPermission="'jdds:plsh'" @click="showCheck">批量审核</a-button>
              <a-button type="primary" @click="exportExcel">导出</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {yxm: selectMenu.yx.length === 1 ? queryParam.yxm : ''}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      v-if="!visible"
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :pageSize="parameter.pageSize"
      :pageNum="parameter.pageNum"
      :scroll="{x: 1600}"
      :rowSelection="options.rowSelection"
    >

      <template slot="xh" slot-scope="a,b">
         <a @click="direct(b)">{{a}}</a>
      </template>
      <template slot="operation" slot-scope="a,b">
        <!-- <a @click="cancelRequest(b)">删除</a> -->
        <a-button type="link" @click="showCheck(b)" :disabled="b.autstate == '已通过' || b.autstate == '未通过'">审核</a-button>
      </template>
    </s-table>
    <check-modal
          ref="checkModal"
          :visible.sync="checkVisible"
          @close="() => { checkVisible = false }"
          @save="batchCheck"
        />
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'
import moment from 'moment'
import checkModal from './component/checkModal'

export default {
  name: 'TableList',
  components: {
    STable,checkModal
  },
  data () {
    return {
      modelShow:false,
      // 高级搜索 展开/关闭
      advanced: true,
      // 查询参数
      queryParam: {},
      studentInfo:{},//学生信息
      postParams: {},
      stuInfo: {},
      // 表头
      columns: [
        // {
        //   title: '序号',
        //   scopedSlots: { customRender: 'serial' }
        // },
        {
          title: '学号',
          dataIndex: 'xh',
          width:150
        },
        {
          title: '姓名',
          dataIndex: 'xm',
          width:100
        },
        {
          title: '贷款年度',
          dataIndex: 'dknd'
        },
        {
          title: '贷款日期',
          dataIndex: 'createDate'
        },
        {
          title: '贷款金额',
          dataIndex: 'dkje',
        },
        {
          title: '年级',
          dataIndex: 'sznj',
        },
        {
          title: '院系',
          dataIndex: 'yxm',
        },
        {
          title: '专业',
          dataIndex: 'zym',
        },
        {
          title: '审核状态',
          dataIndex: 'autstate',
        },
        {
          title: '操作',
          fixed: 'right',
          scopedSlots: { customRender: 'operation' },
          width: '150px'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      selectMenu: {
        yx: [], // 院系
        zy: [] // 所属专业
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.student.studentloanList(params).then(res => {
          if(res.data.rows){
             res.data.rows.map((item=>{
                item.createDate = moment(item.createDate).format('YYYY-MM-DD')
                //item.entTime = moment(item.entTime).format('YYYY-MM-DD')
             }))
          }
          return {
            data: res.data.rows || [],
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      checkId:'',
      optionAlertShow: true, // 是否显示alert，已选中
      visible: false,
      checkVisible: false,
    }
  },
  computed: {
    ...mapState({
      TYPE0125: state => state.app['TYPE0125'],
      TYPE0402: state => state.app['TYPE0402'],
    })
  },
  created () {
    this.$store.dispatch('app/sysInfo').then(data => {
      this.$set(this.queryParam, 'xn', data.schoolYear)
    })
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0402')
    this.initSelectMenu()
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  methods: {
    refresh() {
      this.$refs.table.refresh(false)
    },
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'yxm', this.selectMenu.yx[0].dwh)
          this.yxChange(this.queryParam.yxm)
        }
        this.initDone = true
      })
    },
    yxChange (code) {
      this.$set(this.queryParam, 'zym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    showCheck (data) {
      console.log(data.id)
      if(data.id){
        this.checkId = data.id
        this.checkVisible = true
      }else{
        if (!this.selectedRowKeys.length) {
          this.$message.warning('请选择需要审核的数据')
          return
        }
        this.checkId = ""
        this.checkVisible = true
      }
    },
    batchCheck (params) {
      this.$refs.checkModal.confirmLoading = true
      let dsbhs = []
      if(this.checkId){
        //单个审核
          dsbhs = [this.checkId]
      }else{
        //多选审核
         dsbhs = this.selectedRows.map(ds => {
          return ds.id
        })
      }
      this.$api.student.studentloanAut(Object.assign({ids: dsbhs}, params)).then(() => {
        this.$message.success('操作成功')
        this.selectedRowKeys = []
        this.selectedRows = []
        this.checkVisible = false
        this.$refs.table.refresh(false)
        this.$refs.table.clearSelected()
        this.$refs.checkModal.form.resetFields()
      }).finally(() => {
        this.$refs.checkModal.confirmLoading = false
      })
    },
    handleOk(){

      this.$api.student.studentInfo(this.queryParam.studentId).then(res => {
          this.studentInfo = res.data
          this.visible = true
          this.modelShow = false
        }).catch(e => {

        })
    },
    handleCancel(e) {
      this.modelShow = false;
    },
    sub () {
      const form = this.form
      form.validateFields((err, values) => {
        if (err) {
          return
        }
        let params = Object.assign(values)
        params.autstate = '01'
        params.xh = this.queryParam.studentId
        this.$DateToString(params)
        this.$api.student.studentLoanAdd(params).then(res => {
          this.$refs.table.refresh(true)
          form.resetFields()
          this.visible = false
        }).catch(e => {
          this.visible = false
          this.$message.success(res.msg)
          this.$refs.table.refresh(true)
          form.resetFields()
        })
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    // direct (data) {
    //   this.$router.push(`/student/stuStatus/search/stuView?xh=${data.xh}`)
    // },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    cancelRequest (row) {
      this.$api.student.studentLeaveCancel(row.id).then(res => {
        this.$message.success(res.msg)
        this.$refs.table.refresh(true)
      })
    },
    exportExcel () {
      let params = this.queryParam
      this.$export('/admissions/dksq/export', {
        ...params
      })
    }
  }
}
</script>
<style lang="less">
.ccslh{
      table {
        table-layout:fixed;
      }
      tr td:nth-child(4){
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      tr td:nth-child(5){
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
}

</style>
